package com.stylefeng.guns.rest.modular.film;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.rpc.RpcContext;
import com.stylefeng.guns.api.film.*;
import com.stylefeng.guns.rest.modular.film.vo.FilmConditionVo;
import com.stylefeng.guns.rest.modular.film.vo.FilmIndexVo;
import com.stylefeng.guns.rest.modular.film.vo.FilmRequestVo;
import com.stylefeng.guns.rest.modular.vo.ResponseVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

/**
 * @ClassName FilmController
 * @Description TODO
 * @Author zhaoluowei
 * @Date 2019/9/13  11:50
 * @Version 1.0
 */
@Slf4j
@RestController
@RequestMapping(value = "/film")
public class FilmController {
    private static final String img_pre = "http://img.meetingshop.cn/";

    //注入service
    @Reference(interfaceClass = FilmServiceApi.class, check = false)
    private FilmServiceApi filmServiceApi;

    @Reference(interfaceClass = FilmAsyncServiceApi.class, check = false, async = true)
    private FilmAsyncServiceApi filmAsyncServiceApi;


    /**
     * 获取首页信息接口
     * api网关的重要功能
     * 接口功能聚合
     * --> 同时异步加载6个接口的数据
     *
     * @return
     */
    @RequestMapping(value = "/getIndex", method = RequestMethod.GET)
    public ResponseVo getIndex() {

        FilmIndexVo filmIndexVo = new FilmIndexVo();

        //banner l轮播图
        filmIndexVo.setBanners(filmServiceApi.getBanners());

        //获取热映的影片
        filmIndexVo.setHotFilms(filmServiceApi.getHotFilms(true, 8, 1, 1, 99, 99, 99));
        //即将上映
        filmIndexVo.setSoonFilms(filmServiceApi.getSoonFilms(true, 8, 1, 1, 99, 99, 99));
        //票房排行榜
        filmIndexVo.setBoxRanking(filmServiceApi.getBoxRanking());
        //受欢迎的人气榜 榜单
        filmIndexVo.setExpectRanking(filmServiceApi.getExpectRanking());
        //获取 前100
        filmIndexVo.setTop100(filmServiceApi.getTop());

        return ResponseVo.success(img_pre, filmIndexVo);
    }

    @RequestMapping(value = "/getConditionList", method = RequestMethod.POST)
    public ResponseVo getConditionList(@RequestParam(value = "catId", defaultValue = "99", required = false) String catId,
                                       @RequestParam(value = "sourceId", defaultValue = "99", required = false) String sourceId,
                                       @RequestParam(value = "yearId", defaultValue = "99", required = false) String yearId) {

        System.out.println("catId --> " + catId);
        System.out.println("sourceId --> " + sourceId);
        System.out.println("yearId --> " + yearId);
        FilmConditionVo filmConditionVo = new FilmConditionVo();

        //类型集合
        boolean flag = false;
        List<CatVo> cats = filmServiceApi.getCats();
        List<CatVo> catResult = new ArrayList<>();
        catResult.clear();

        CatVo catVo = null;
        for (CatVo cat : cats) {
            if (cat.getCatId().equals("99")) {
                catVo = cat;
                continue;
            }
            if (cat.getCatId().equals(catId)) {
                flag = true;
                cat.setActive(true);
            } else {
                cat.setActive(false);
            }

            catResult.add(cat);
        }

        if (!flag) {
            catVo.setActive(true);
            catResult.add(catVo);
        } else {
            catVo.setActive(false);
            catResult.add(catVo);
        }


        //片源集合
        flag = false;
        List<SourceVo> sources = filmServiceApi.getSources();
        List<SourceVo> sourceResult = new ArrayList<>();
        sourceResult.clear();
        SourceVo sourceVo = null;
        for (SourceVo source : sources) {
            if (source.getSourceId().equals("99")) {
                sourceVo = source;
                continue;
            }
            if (source.getSourceId().equals(sourceId)) {
                flag = true;
                source.setActive(true);
            } else {
                source.setActive(false);
            }

            sourceResult.add(source);

        }

        if (!flag) {
            sourceVo.setActive(true);
            sourceResult.add(sourceVo);
        } else {
            sourceVo.setActive(false);
            sourceResult.add(sourceVo);
        }


        //年代集合

        flag = false;
        List<YearVo> years = filmServiceApi.getYears();
        List<YearVo> yearResult = new ArrayList<>();
        yearResult.clear();
        YearVo yearVo = null;

        for (YearVo year : years) {
            if (year.getYearId().equals("99")) {
                yearVo = year;
                continue;
            }
            if (year.getYearId().equals(yearId)) {
                flag = true;
                year.setActive(true);
            } else {
                year.setActive(false);
            }

            yearResult.add(year);

        }
        if (!flag) {
            yearVo.setActive(true);
            yearResult.add(yearVo);
        } else {
            yearVo.setActive(false);
            yearResult.add(yearVo);
        }
        filmConditionVo.setCatInfo(catResult);
        filmConditionVo.setSourceInfo(sourceResult);
        filmConditionVo.setYearInfo(yearResult);
        return ResponseVo.success(filmConditionVo);
    }

    /**
     * 影片条件查询
     * 方法的重构
     * --> api的重构
     * 怎么重构
     *
     * @return
     */
    @RequestMapping(value = "/getFilms", method = RequestMethod.POST)
    public ResponseVo getFilms(FilmRequestVo request) {

        FilmsVo filmsVo = null;
        String img_pre = "http://img.meetingshop.cn/";
        //根据showtype 判断影片  查询 类型
        //根据 sortId 进行排序
        //添加各种查询条件
        //判断当前是第几页
        switch (request.getShowType()) {
            case 1:
                filmsVo = filmServiceApi.getHotFilms(false, request.getPageSize(),
                        request.getNowPage(), request.getSortId(), request.getSourceId(), request.getYearId(), request.getCatId());
                break;
            case 2:
                filmsVo = filmServiceApi.getSoonFilms(false, request.getPageSize(),
                        request.getNowPage(), request.getSortId(), request.getSourceId(), request.getYearId(), request.getCatId());

                break;
            case 3:
                filmsVo = filmServiceApi.getClassicFilms(request.getPageSize(),
                        request.getNowPage(), request.getSortId(), request.getSourceId(), request.getYearId(), request.getCatId());
                break;
            default:
                filmsVo = filmServiceApi.getHotFilms(false, request.getPageSize(),
                        request.getNowPage(), request.getSortId(), request.getSourceId(), request.getYearId(), request.getCatId());
                break;


        }
        return ResponseVo.success(filmsVo.getTotalPage(), filmsVo.getNowPage(), img_pre, filmsVo.getFilmInfo());
    }


    //根据影片id或者名称获取  影片详情接口
    @RequestMapping(value = "/films/{searchParam}", method = RequestMethod.GET)
    public ResponseVo getFilmInfo(@PathVariable("searchParam") String searchParam,
                                  @RequestParam("searchType") int searchType) throws ExecutionException, InterruptedException {
        //根据searchType 判断查询类型 不同的类型走不同的接口
        //按照id去查询 --> 走redis MySQL
        //按名称去查  --> 走搜索引擎 Solor elasticsearch
        //需要查询 电影的详细信息。 --> dubbo的异步获取的特性
        log.info("searchParam --> " + searchParam);
        log.info("searchType --> " + searchType);

        //searchType --> 1 按名称 2 按id
        FilmDetailVo filmDetail = filmServiceApi.getFilmDetail(searchType, searchParam);
        if (filmDetail == null) {
            return ResponseVo.serviceFail("查询不到影片");

        } else if (filmDetail == null || filmDetail.getFilmId().trim().length() == 0) {
            return ResponseVo.serviceFail("查询不到影片");
        }


        String filmId = filmDetail.getFilmId();
//        //获取影片描述信息
//        FilmDesVo filmDes = filmServiceApi.getFilmDes(filmId);
//
//        //获取图片信息
//        ImgVo imgs = filmServiceApi.getImgs(filmId);
//
//        //获取导演信息
//        ActorVo dectInfo = filmServiceApi.getDectInfo(filmId);
//
//        List<ActorVo> actors = filmServiceApi.getActors(filmId);


        //测试异步调用
        //获取影片描述信息
        //FilmDesVo filmDes = filmAsyncServiceApi.getFilmDes(filmId);
        filmAsyncServiceApi.getFilmDes(filmId);
        Future<FilmDesVo> future01 = RpcContext.getContext().getFuture();

        //获取图片信息
        //ImgVo imgs = filmAsyncServiceApi.getImgs(filmId);
        filmAsyncServiceApi.getImgs(filmId);
        Future<ImgVo> future02 = RpcContext.getContext().getFuture();

        //获取导演信息
        //ActorVo dectInfo = filmAsyncServiceApi.getDectInfo(filmId);
        filmAsyncServiceApi.getDectInfo(filmId);
        Future<ActorVo> future03 = RpcContext.getContext().getFuture();

        //List<ActorVo> actors = filmAsyncServiceApi.getActors(filmId);
        filmAsyncServiceApi.getActors(filmId);
        Future<List<ActorVo>> future04 = RpcContext.getContext().getFuture();


        //组装数据
        InfoRequestVo infoRequestVo = new InfoRequestVo();

        //组织 actor 属性
        ActorRequestVo actorRequestVo = new ActorRequestVo();
        actorRequestVo
//                .setActors(actors)
//                .setDirector(dectInfo);
                .setActors(future04.get())
                .setDirector(future03.get());

        infoRequestVo
                .setActors(actorRequestVo)
                .setBiography(future01.get().getBiography())
                .setFilmId(future01.get().getFilmId())
                .setImgs(future02.get());

        filmDetail.setInfo04(infoRequestVo);

        return ResponseVo.success(img_pre, filmDetail);
    }


}
